package cn.player.act;

import android.content.Context;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;
import android.widget.Toast;

import net.tsz.afinal.FinalBitmap;
import net.tsz.afinal.FinalDb;
import net.tsz.afinal.FinalHttp;
import net.tsz.afinal.http.AjaxCallBack;

import org.json.JSONObject;

import java.io.File;

import cn.player.App;
import cn.player.R;

public abstract class BaseActivity extends AppCompatActivity {

    public final int LAND = ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE;
    public final int PORT = ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT;

    public Context mContext;
    public App app;
    public FinalHttp finalHttp;
    public FinalBitmap finalBitmap;
    public FinalDb finalDb;

    public static boolean mP2pInitComplete = false;
    public static boolean mP2pInitSuccess = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);

        mContext = this;
        app = (App) getApplicationContext();
        finalHttp = app.getFinalHttp();
        finalBitmap = app.getFinalBitmap();
//		finalBitmap.configLoadingImage(R.drawable.corner_test_icon);//for test

        finalDb = app.getFinalDb();

    }


    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
    }

    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
    }

    protected void initJsonData(final String url) {
        initJsonData(url, false);
    }

    /**
     * Afinal访问网络获取数据，子类调用此方法 回调initDataSuccess(String url,JSONObject response)/initDataSuccess(String url,String response)和initDataFailed()
     *
     * @param url
     * @param isHandleException 是否处理异常，即获取到的数据不是正常的json串
     *                          true 调用 initDataSuccess(String url,String response)
     *                          false 调用 initDataFailed()
     */
    protected void initJsonData(final String url, final boolean isHandleException) {
        if (url == null || url.trim().length() == 0) {
            initDataFailed(url, 103, getString(R.string.request_url_null));
            return;
        }
        finalHttp.get(url, new AjaxCallBack<String>() {
            @Override
            public void onStart() {
                // TODO Auto-generated method stub
                super.onStart();
            }

            @Override
            public void onSuccess(String result) {
                // TODO Auto-generated method stub
                super.onSuccess(result);
                try {
                    if (result == null || "".equals(result)) {
                        initDataFailed(url, 101, getString(R.string.server_data_null));
                    } else {
                        initDataSuccess(url, new JSONObject(result));
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    if (isHandleException) {
                        initDataSuccess(url, result);
                    } else {
                        initDataFailed(url, 102, getString(R.string.server_data_exception));
                    }
                }
            }

            @Override
            public void onFailure(Throwable t, int errorNo, String strMsg) {
                // TODO Auto-generated method stub
                super.onFailure(t, errorNo, strMsg);
                Toast.makeText(mContext, "msg = " + strMsg, Toast.LENGTH_SHORT).show();
                initDataFailed(url, errorNo, strMsg);
            }

        });
    }

    /**
     * 下载文件
     *
     * @param url       成功功回调 initDataSuccess(String url,File file方法
     * @param targetUrl 文件保存的地址
     */
    protected void downloadFile(final String url, String targetUrl) {
        finalHttp.download(url, targetUrl, new AjaxCallBack<File>() {

            @Override
            public boolean isProgress() {
                // TODO Auto-generated method stub
                return true;
            }

            @Override
            public int getRate() {
                // TODO Auto-generated method stub
                return 1000;//1000ms刷新一次
            }

            @Override
            public void onStart() {
                // TODO Auto-generated method stub
                super.onStart();
            }

            @Override
            public void onLoading(long count, long current) {
                // TODO Auto-generated method stub
                super.onLoading(count, current);
            }

            @Override
            public void onSuccess(File file) {
                // TODO Auto-generated method stub
                super.onSuccess(file);
                if (file != null) {
                    initDataSuccess(url, file);
                } else {
                    initDataFailed(url, 102, getString(R.string.server_data_exception));
                }
            }

            @Override
            public void onFailure(Throwable t, int errorNo, String strMsg) {
                // TODO Auto-generated method stub
                super.onFailure(t, errorNo, strMsg);
                Toast.makeText(mContext, "msg = " + strMsg, Toast.LENGTH_SHORT).show();
                initDataFailed(url, errorNo, strMsg);
            }
        });
    }

    protected void initDataSuccess(String url, JSONObject response) {
    }

    protected void initDataSuccess(String url, File file) {
    }

    protected void initDataSuccess(String url, String response) {
    }

    protected void initDataFailed(String url, int errorNo, String strMsg) {
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        // TODO Auto-generated method stub
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
            this.finish();
            overridePendingTransition(R.anim.base_fade_in, R.anim.base_push_right_out);
            return false;
        } else {
            return super.onKeyDown(keyCode, event);
        }
    }

}
